package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;

import com.example.myapplication.service.MusicService;

public class MusicActivity extends AppCompatActivity {

    private Intent it;

    private MusicService.MusicBinder binder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);
        it = new Intent(MusicActivity.this, MusicService.class);
        class connection implements ServiceConnection {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Log.i("Music","service connected");
                if(iBinder!=null)
                    binder=(MusicService.MusicBinder)iBinder;
            }
            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                binder=null;
            }
        }
        connection connection=new connection();
        View.OnClickListener listener=new View.OnClickListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public void onClick(View view) {
                switch (view.getId()){
                    case R.id.service_start:{
                        it.putExtra("name",R.raw.music);
                        bindService(it,connection,BIND_AUTO_CREATE);
                        break;
                    }
                    case R.id.hello:{
                        binder.start(R.raw.music);
                        break;
                    }
                    /*case R.id.remix: {
                        binder.start(R.id.write);
                        break;
                    }*/
                    case R.id.pause:{
                        Button button = findViewById(R.id.pause);
                        if(button.getText().equals("暂停播放")){
                            binder.pause();
                            button.setText("继续播放");
                        }
                        else if(button.getText().equals("继续播放")){
                            binder.continues();
                            button.setText("暂停播放");
                        }
                        break;
                    }
                    case R.id.stop:{
                        binder.stop();
                        break;
                    }
                    case R.id.loop:{
                        binder.setLooping(((Switch)findViewById(R.id.loop)).isChecked());
                    }
                    case R.id.service_terminal:{
                        unbindService(connection);
                        Intent intent = new Intent();
                        intent.setClass(MusicActivity.this,MainActivity.class);
                        startActivity(intent);
                        break;
                    }
                }
            }
        };
        findViewById(R.id.service_start).setOnClickListener(listener);
        findViewById(R.id.hello).setOnClickListener(listener);
        //findViewById(R.id.remix).setOnClickListener(listener);
        findViewById(R.id.pause).setOnClickListener(listener);
        findViewById(R.id.stop).setOnClickListener(listener);
        findViewById(R.id.loop).setOnClickListener(listener);
        findViewById(R.id.service_terminal).setOnClickListener(listener);
    }
}